package com.ruoyi.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.admin.domain.ZhxLikeComment;
import com.ruoyi.admin.domain.ZhxNote;
import com.ruoyi.admin.domain.ZhxTopic;
import com.ruoyi.admin.mapper.ZhxNoteMapper;
import com.ruoyi.admin.mapper.ZhxTopicMapper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.admin.mapper.ZhxLikeTopicMapper;
import com.ruoyi.admin.domain.ZhxLikeTopic;
import com.ruoyi.admin.service.IZhxLikeTopicService;

/**
 * 用户点赞话题Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-11
 */
@Service
public class ZhxLikeTopicServiceImpl implements IZhxLikeTopicService 
{
    @Autowired
    private ZhxLikeTopicMapper zhxLikeTopicMapper;

    @Autowired
    private ZhxTopicMapper zhxTopicMapper;

    @Autowired
    private ZhxNoteMapper zhxNoteMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询用户点赞话题
     * 
     * @param likeTopicId 用户点赞话题主键
     * @return 用户点赞话题
     */
    @Override
    public ZhxLikeTopic selectZhxLikeTopicByLikeTopicId(Long likeTopicId)
    {
        return zhxLikeTopicMapper.selectZhxLikeTopicByLikeTopicId(likeTopicId);
    }

    /**
     * 查询用户点赞话题列表
     * 
     * @param zhxLikeTopic 用户点赞话题
     * @return 用户点赞话题
     */
    @Override
    public List<ZhxLikeTopic> selectZhxLikeTopicList(ZhxLikeTopic zhxLikeTopic)
    {
        return zhxLikeTopicMapper.selectZhxLikeTopicList(zhxLikeTopic);
    }

    /**
     * 新增用户点赞话题
     * 
     * @param zhxLikeTopic 用户点赞话题
     * @return 结果
     */
    @Override
    public int insertZhxLikeTopic(ZhxLikeTopic zhxLikeTopic)
    {
        zhxLikeTopic.setCreateTime(DateUtils.getNowDate());
        return zhxLikeTopicMapper.insertZhxLikeTopic(zhxLikeTopic);
    }

    /**
     * 新增用户点赞话题
     * 话题点赞数+1
     * @param zhxLikeTopic 用户点赞话题
     * @return 结果
     */
    @Override
    public int insertZhxLikeTopicTodo(ZhxLikeTopic zhxLikeTopic)
    {
        ZhxTopic zhxTopic = zhxTopicMapper.selectZhxTopicByTopicId(zhxLikeTopic.getTopicId());
        zhxTopic.setLikeNum(zhxTopic.getLikeNum() + 1);
        zhxTopicMapper.updateZhxTopic(zhxTopic);
        zhxLikeTopic.setUserId(SecurityUtils.getUserId());
        zhxLikeTopic.setCreateTime(DateUtils.getNowDate());

        // 新增话题点赞互动消息
        ZhxNote zhxNote = new ZhxNote();
        // 信息类型（1接龙点赞 2评论点赞 3话题点赞 4评论 5回复）
        zhxNote.setNoteType("3");
        zhxNote.setNoteBodyId(zhxLikeTopic.getTopicId());
        zhxNote.setUserId(zhxLikeTopic.getUserId());
        zhxNote.setToUserId(zhxTopic.getUserId());
        zhxNote.setIsRead("0");
        zhxNoteMapper.insertZhxNote(zhxNote);

        // redis数据新增
        Map<String, Long> zhxLikeTopicCache = new HashMap<>();
        zhxLikeTopicCache = redisCache.getCacheObject("zhxLikeTopicCache");
        zhxLikeTopicCache.put(zhxLikeTopic.getUserId() + "-" + zhxLikeTopic.getTopicId(),1L);
        redisCache.setCacheObject("zhxLikeTopicCache", zhxLikeTopicCache);

        return zhxLikeTopicMapper.insertZhxLikeTopic(zhxLikeTopic);
    }

    /**
     * 修改用户点赞话题
     * 
     * @param zhxLikeTopic 用户点赞话题
     * @return 结果
     */
    @Override
    public int updateZhxLikeTopic(ZhxLikeTopic zhxLikeTopic)
    {
        return zhxLikeTopicMapper.updateZhxLikeTopic(zhxLikeTopic);
    }

    /**
     * 批量删除用户点赞话题
     * 
     * @param likeTopicIds 需要删除的用户点赞话题主键
     * @return 结果
     */
    @Override
    public int deleteZhxLikeTopicByLikeTopicIds(Long[] likeTopicIds)
    {
        return zhxLikeTopicMapper.deleteZhxLikeTopicByLikeTopicIds(likeTopicIds);
    }

    /**
     * 删除用户点赞话题信息
     * 
     * @param likeTopicId 用户点赞话题主键
     * @return 结果
     */
    @Override
    public int deleteZhxLikeTopicByLikeTopicId(Long likeTopicId)
    {
        return zhxLikeTopicMapper.deleteZhxLikeTopicByLikeTopicId(likeTopicId);
    }

    /**
     * 删除用户点赞话题信息
     * 话题点赞数-1
     * @param likeTopicId 用户点赞话题主键
     * @return 结果
     */
    @Override
    public int deleteZhxLikeTopicByLikeTopicIdTodo(Long likeTopicId)
    {
        ZhxLikeTopic zhxLikeTopic = zhxLikeTopicMapper.selectZhxLikeTopicByLikeTopicId(likeTopicId);
        ZhxTopic zhxTopic = zhxTopicMapper.selectZhxTopicByTopicId(zhxLikeTopic.getTopicId());
        zhxTopic.setLikeNum(zhxTopic.getLikeNum() - 1);
        zhxTopicMapper.updateZhxTopic(zhxTopic);

        // 删除话题点赞互动消息
        ZhxNote zhxNote = new ZhxNote();
        // 信息类型（1接龙点赞 2评论点赞 3话题点赞 4评论 5回复）
        zhxNote.setNoteType("3");
        zhxNote.setNoteBodyId(zhxLikeTopic.getTopicId());
        zhxNote.setUserId(zhxLikeTopic.getUserId());
        zhxNoteMapper.deleteZhxNoteByNoteInfo(zhxNote);

        // redis数据删除
        Map<String, Long> zhxLikeTopicCache = new HashMap<>();
        zhxLikeTopicCache = redisCache.getCacheObject("zhxLikeTopicCache");
        zhxLikeTopicCache.remove(zhxLikeTopic.getUserId() + "-" + zhxLikeTopic.getTopicId(),1L);
        redisCache.setCacheObject("zhxLikeTopicCache", zhxLikeTopicCache);

        return zhxLikeTopicMapper.deleteZhxLikeTopicByLikeTopicId(likeTopicId);
    }

}
